package figure8.GUI;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;
import javax.swing.border.EtchedBorder;

import figure8.Controller;
import figure8.LoopTiming;

public class TimingSettingsWindow extends JDialog {
	public TimingSettingsWindow (final Controller controller) {
		super();

		final JDialog projectWindow = this;	
		final JPanel panel = new JPanel(new GridLayout(5, 2, 2, 2));
		panel.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));
		
		this.getContentPane().add(panel);

		panel.add(new JLabel("Tempo"));
		final SpinnerNumberModel tempo = new SpinnerNumberModel(120, 1, 400,
				1);
		panel.add(new JSpinner(tempo));

		panel.add(new JLabel("Bars Per Loop"));
		final SpinnerNumberModel bars = new SpinnerNumberModel(4, 1, 128, 1);
		panel.add(new JSpinner(bars));

		panel.add(new JLabel("Time Signature"));

		JPanel time = new JPanel(new GridLayout(1, 2));
		final SpinnerNumberModel note = new SpinnerNumberModel(4, 1, 32, 1);
		final SpinnerNumberModel beats = new SpinnerNumberModel(4, 1, 32, 1);
		time.add(new JSpinner(note));
		time.add(new JSpinner(beats));

		panel.add(time);

		final JButton submit = new JButton("Submit");
		submit.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				LoopTiming timing = new LoopTiming((int) tempo.getNumber().intValue(), (int) bars
						.getNumber().intValue(), note.getNumber().intValue(), (int) beats.getNumber().intValue());
				controller.updateTiming(timing);
				
				projectWindow.dispose();
			}
		});

		panel.add(submit);

		this.pack();
		this.setVisible(true);
	}
}
